package oas.web;

import org.springframework.beans.factory.annotation.Autowired;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import oas.db.LeaveTypeRepository;
import oas.domain.LeaveType;

/**
 * 请假类型的控制器
 *
 * @author wjy
 * @version v1.0.0
 */
@Controller
@RequestMapping("/leaveTypes")
public class LeaveTypesController {

    @Autowired
    LeaveTypeRepository leaveTypeRepository;

    private static Logger logger = Logger.getLogger("oas");

    /**
     * 分页显示所有的请假类型
     *
     * @param pageNo   起始页面
     * @param pageSize 页面大小
     * @param model    前端使用的model
     * @return
     */
    @RequestMapping(method = GET)
    public String showLeaveTypes(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                 @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                 @RequestParam(value = "message", required = false) String message, Model model) {

        PaginationSupport<LeaveType> ps = leaveTypeRepository.findPage(pageNo, pageSize);

        logger.debug(ps.getItems().toString());
        model.addAttribute("paginationSupport", ps);
        model.addAttribute("message", message);

        return "leaveTypes";
    }

    /**
     * 删除一个请假类型
     *
     * @param id    要删除请假类的id
     * @param model
     * @return
     */
    @RequestMapping(value = "/delete", method = GET)
    public String deleteALeaveType(long id, Model model) {

        logger.debug(id);
        leaveTypeRepository.deleteOne(id);
        return "redirect:/leaveTypes";
    }

    /**
     * 更新一个请假类型
     *
     * @param request 用户的请求
     * @return
     */
    @RequestMapping(value = "/update", method = POST)
    public String updateALeaveType(HttpServletRequest request, Model model) {

        Long id = Long.parseLong(request.getParameter("id"));
        String description = request.getParameter("description");
        logger.debug("update " + id + ": " + description);
        leaveTypeRepository.update(new LeaveType(id, description, true));
        model.addAttribute("message", "201");
        return "redirect:/leaveTypes";
    }

    /**
     * 添加一个请假类型
     *
     * @param request 用户的请求
     * @return
     */
    @RequestMapping(value = "/add", method = POST)
    public String addALeaveType(HttpServletRequest request, Model model) {

        String description = request.getParameter("description");
        LeaveType leaveType = leaveTypeRepository.save(new LeaveType(null, description, true));
        logger.debug("save " + leaveType.getId() + ": " + description);
        model.addAttribute("message", "200");
        return "redirect:/leaveTypes";
    }

}
